package uni.j2ee.forum.controller.beans;

import java.util.Collection;
import javax.ejb.EJB;
import uni.j2ee.forum.ejb.impl.ForumUserFacade;
import uni.j2ee.forum.helper.CommonHelper;
import uni.j2ee.forum.model.ForumUser;
import uni.j2ee.forum.navigation.Navigation;

public class LoginController {

    @EJB
    private uni.j2ee.forum.ejb.impl.ForumUserFacade ejbFacade;
    private String username;
    private String password;

    public String login() {
        return login(ejbFacade, username, password);
    }
    
    public static String login(ForumUserFacade ejbFacade, String username, String password) {
        Collection<ForumUser> users = ejbFacade.findAll();
        for (ForumUser user : users) {
            if (user.getUsername().equalsIgnoreCase(username) && user.getPassword().equals(password)) {
                CommonHelper.putSessionMap("user", user);
                CommonHelper.addMessage("login_ok", false);
                return Navigation.CATEGORIES;
            }
        }

        CommonHelper.addMessage("login_incorrect", true);
        return null;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}
